Lecture 21
benchd = tibble(
x = runif(10000),
y = runif(10000)
)
(b = bench::mark(
d[d$x > 0.5, ],
d[which(d$x > 0.5), ],
subset(d, x > 0.5),
filter(d, x > 0.5)
))# A tibble: 4 × 6
expression min median `itr/sec` mem_alloc `gc/sec`
<bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
1 d[d$x > 0.5, ] 128µs 137µs 7183. 238.81KB 19.3
2 d[which(d$x > 0.5), ] 134µs 148µs 6723. 269.64KB 36.1
3 subset(d, x > 0.5) 166µs 181µs 5441. 287.82KB 26.2
4 filter(d, x > 0.5) 376µs 397µs 2490. 1.47MB 20.6
d = tibble(
x = runif(1e6),
y = runif(1e6)
)
(b = bench::mark(
d[d$x > 0.5, ],
d[which(d$x > 0.5), ],
subset(d, x > 0.5),
filter(d, x > 0.5)
))# A tibble: 4 × 6
expression min median `itr/sec` mem_alloc `gc/sec`
<bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
1 d[d$x > 0.5, ] 11.7ms 12ms 83.3 13.3MB 58.3
2 d[which(d$x > 0.5), ] 13.2ms 13.3ms 75.2 24.8MB 90.2
3 subset(d, x > 0.5) 17.3ms 17.6ms 56.7 24.8MB 70.8
4 filter(d, x > 0.5) 13.3ms 13.7ms 73.4 24.8MB 122.
bench - relative results# A tibble: 4 × 6
expression min median `itr/sec` mem_alloc `gc/sec`
<bch:expr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 d[d$x > 0.5, ] 1 1 1.47 1 1
2 d[which(d$x > 0.5), ] 1.13 1.10 1.33 1.86 1.55
3 subset(d, x > 0.5) 1.48 1.46 1 1.86 1.21
4 filter(d, x > 0.5) 1.13 1.14 1.30 1.86 2.10
t.testImagine we have run 1000 experiments (rows), each of which collects data on 50 individuals (columns). The first 25 individuals in each experiment are assigned to group 1 and the rest to group 2.
The goal is to calculate the t-statistic for each experiment comparing group 1 to group 2.
# A tibble: 50 × 1,002
ind group exp1 exp2 exp3 exp4 exp5 exp6
<int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 1 16.7 12.0 9.77 7.76 10.2 11.5
2 2 1 2.56 8.37 8.65 12.3 6.25 9.51
3 3 1 13.6 12.3 14.3 14.9 13.6 6.11
4 4 1 12.5 8.40 12.5 9.97 9.38 7.14
5 5 1 2.67 9.36 9.86 8.10 8.59 11.1
6 6 1 12.7 8.24 17.6 9.74 10.3 4.64
7 7 1 12.3 11.2 10.8 8.12 7.76 10.3
8 8 1 12.1 8.70 10.8 7.42 6.88 8.58
9 9 1 7.64 4.53 10.9 10.4 8.65 12.1
10 10 1 8.12 11.8 9.79 9.12 12.0 8.57
# ℹ 40 more rows
# ℹ 994 more variables: exp7 <dbl>, exp8 <dbl>,
# exp9 <dbl>, exp10 <dbl>, exp11 <dbl>,
# exp12 <dbl>, exp13 <dbl>, exp14 <dbl>,
# exp15 <dbl>, exp16 <dbl>, exp17 <dbl>,
# exp18 <dbl>, exp19 <dbl>, exp20 <dbl>,
# exp21 <dbl>, exp22 <dbl>, exp23 <dbl>, …
ttest_hand_calc = function(X) {
f = function(x, grp) {
t_stat = function(x) {
m = mean(x)
n = length(x)
var = sum((x - m) ^ 2) / (n - 1)
list(m = m, n = n, var = var)
}
g1 = t_stat(x[grp == 1])
g2 = t_stat(x[grp == 2])
se_total = sqrt(g1$var / g1$n + g2$var / g2$n)
(g1$m - g2$m) / se_total
}
apply(X[,-(1:2)], 2, f, X$group)
}
system.time(ttest_hand_calc(X)) user system elapsed
0.014 0.000 0.015
bench::mark(
ttest_formula(X, m),
ttest_for(X, m),
ttest_apply(X),
ttest_hand_calc(X),
check=FALSE
)# A tibble: 4 × 6
expression min median `itr/sec` mem_alloc `gc/sec`
<bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
1 ttest_formula(X, m) 201.74ms 203.25ms 4.90 8.24MB 29.4
2 ttest_for(X, m) 67.09ms 67.59ms 14.6 1.91MB 31.1
3 ttest_apply(X) 60.55ms 60.92ms 16.4 3.49MB 32.8
4 ttest_hand_calc(X) 8.76ms 9.56ms 87.0 3.45MB 29.7
parallelPart of the base packages in R
tools for the forking of R processes (some functions do not work on Windows)
Core functions:
detectCores
pvec
mclapply
mcparallel & mccollect
detectCoresSurprisingly, detects the number of cores of the current system.
Parallelization of a vectorized function call
user system elapsed
0.095 0.011 0.106
user system elapsed
0.167 0.126 0.242
user system elapsed
0.089 0.170 0.155
bench::system_time 10^6 10^7 10^8
1 cores 0.004 0.024 0.320
4 cores 0.039 0.146 1.879
6 cores 0.032 0.127 1.323
8 cores 0.036 0.138 1.519
10 cores 0.036 0.164 1.434
Parallelized version of lapply
Asynchronously evaluation of an R expression in a separate process
Checks mcparallel objects for completion
Packages by Revolution Analytics that provides the foreach function which is a parallelizable for loop (and then some).
Core functions:
registerDoMC
foreach, %dopar%, %do%
registerDoMCPrimarily used to set the number of cores used by foreach, by default uses options("cores") or half the number of cores found by detectCores from the parallel package.
foreachA slightly more powerful version of base for loops (think for with an lapply flavor). Combined with %do% or %dopar% for single or multicore execution.
foreach - iteratorsforeach can iterate across more than one value, but it doesn’t do length coercion
foreach - combining resultsforeach - parallelizationSwapping out %do% for %dopar% will use the parallel backend.
user system elapsed
0.298 0.022 0.108
user system elapsed
0.303 0.038 0.078
user system elapsed
0.324 0.050 0.066
user system elapsed
0.000 0.000 3.012
user system elapsed
0.050 0.006 3.084
Bootstrapping is a resampling scheme where the original data is repeatedly reconstructed by taking a samples of size n (with replacement) from the original data, and using that to repeat an analysis procedure of interest. Below is an example of fitting a local regression (loess) to some synthetic data, we will construct a bootstrap prediction interval for this model.
Optimal use of parallelization / multiple cores is hard, there isn’t one best solution
Don’t underestimate the overhead cost
Experimentation is key
Measure it or it didn’t happen
Be aware of the trade off between developer time and run time
An awful lot of statistics is at its core linear algebra.
For example:
\[ \hat{\beta} = (X^T X)^{-1} X^Ty \]
Principle component analysis
Find \(T = XW\) where \(W\) is a matrix whose columns are the eigenvectors of \(X^TX\).
Often solved via SVD - Let \(X = U\Sigma W^T\) then \(T = U\Sigma\).
Not unique to Statistics, these are the type of problems that come up across all areas of numerical computing.
Numerical linear algebra \(\ne\) mathematical linear algebra
Efficiency and stability of numerical algorithms matter
Don’t reinvent the wheel - common core linear algebra tools (well defined API)
Low level algorithms for common linear algebra operations
Basic Linear Algebra Subprograms
Copying, scaling, multiplying vectors and matrices
Origins go back to 1979, written in Fortran
Linear Algebra Package
Higher level functionality building on BLAS.
Linear solvers, eigenvalues, and matrix decompositions
Origins go back to 1992, mostly Fortran (expanded on LINPACK, EISPACK)
Most default BLAS and LAPACK implementations (like R’s defaults) are somewhat dated
Written in Fortran and designed for a single cpu core
Certain (potentially non-optimal) hard coded defaults (e.g. block size).
Multithreaded alternatives:
ATLAS - Automatically Tuned Linear Algebra Software
OpenBLAS - fork of GotoBLAS from TACC at UTexas
Intel MKL - Math Kernel Library, part of Intel’s commercial compiler tools
cuBLAS / Magma - GPU libraries from Nvidia and UTK respectively
Accelerate / vecLib - Apple’s framework for GPU and multicore computing
| n | 1 core | 2 cores | 4 cores | 8 cores | 16 cores |
|---|---|---|---|---|---|
| 100 | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 |
| 500 | 0.004 | 0.003 | 0.002 | 0.002 | 0.004 |
| 1000 | 0.028 | 0.016 | 0.010 | 0.007 | 0.009 |
| 2000 | 0.207 | 0.110 | 0.058 | 0.035 | 0.039 |
| 3000 | 0.679 | 0.352 | 0.183 | 0.103 | 0.081 |
| 4000 | 1.587 | 0.816 | 0.418 | 0.227 | 0.145 |
| 5000 | 3.104 | 1.583 | 0.807 | 0.453 | 0.266 |
Sta 323 - Spring 2024